<?php
/**
 * +----------------------------------------------------------------------
 * | summer framework
 * +----------------------------------------------------------------------
 * | Copyright (c) 2020 https://github.com/suframe/think-admin All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: summer <806115620@qq.com>  2020/3/26 10:40
 * +----------------------------------------------------------------------
 */
declare (strict_types=1);

namespace app\mall\logic\order;

use app\mall\enum\OpUserEnum;
use app\mall\enum\OrderRefundEnum;
use app\mall\enum\OrderStatusEnum;
use app\mall\Events;
use app\mall\exceptions\BaseException;
use app\mall\exceptions\GoodsException;
use app\mall\exceptions\OrderException;
use app\mall\exceptions\UserException;
use app\mall\logic\GoodsLogic;
use app\mall\model\MallGoodsStock;
use app\mall\model\MallOrder;
use app\mall\model\MallOrderLog;
use app\mall\model\MallOrderPay;
use app\mall\model\MallStore;
use app\mall\model\MallStoreClerk;
use app\mall\model\MallUser;
use app\mall\model\MallUserAddress;
use suframe\thinkAdmin\model\AdminUsers;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;

class OrderAdminLogic extends Base
{

    /**
     * 管理员取消订单
     * @param MallOrder $mallOrder
     * @param AdminUsers $adminUsers
     * @param string $reason
     * @return bool
     * @throws BaseException
     */
    public function cancel(MallOrder $mallOrder, AdminUsers $adminUsers, string $reason = ''): bool
    {
        return $this->doCancel(
            $mallOrder,
            OpUserEnum::ADMIN,
            $adminUsers->id,
            "管理员取消订单" . ($reason ? ':' . $reason : '')
        );
    }

    /**
     * 管理员发货
     * @param MallOrder $mallOrder
     * @param AdminUsers $adminUsers
     * @param int $mallLogisticsCompanyId
     * @param string $logistics_sn
     * @return bool
     */
    public function send(MallOrder $mallOrder, AdminUsers $adminUsers, int $mallLogisticsCompanyId, string $logistics_sn)
    {
        return $this->doSend(
            $mallOrder,
            $mallLogisticsCompanyId,
            $logistics_sn,
            OpUserEnum::ADMIN,
            $adminUsers->id,
            '管理员发货'
        );
    }

    /**
     * 管理员退款
     * @param bool $audit 审核结果，true通过，false拒绝
     * @param MallOrder $mallOrder
     * @param AdminUsers $adminUsers
     * @param string $desc
     * @return bool
     * @throws BaseException
     */
    public function refund(bool $audit, MallOrder $mallOrder, AdminUsers $adminUsers, string $desc = ''): bool
    {
        return $this->doRefund(
            $audit,
            $mallOrder,
            OpUserEnum::ADMIN,
            $adminUsers->id,
            $desc
        );
    }

    /**
     * 管理员完成订单
     * @param MallOrder $mallOrder
     * @param AdminUsers $adminUsers
     * @param string $reason
     * @return bool
     * @throws BaseException
     */
    public function complete(MallOrder $mallOrder, AdminUsers $adminUsers, string $reason = ''): bool
    {
        return $this->doComplete(
            $mallOrder,
            OpUserEnum::ADMIN,
            $adminUsers->id,
            "管理员完成订单" . ($reason ? ':' . $reason : '')
        );
    }

    /**
     * 管理员完成订单
     * @param MallOrder $mallOrder
     * @param AdminUsers $adminUsers
     * @param int $rate
     * @param string $content
     * @param array $images
     * @return bool
     * @throws BaseException
     */
    public function evaluate(MallOrder $mallOrder, AdminUsers $adminUsers, int $rate, string $content, array $images = []): bool
    {
        return $this->doEvaluate(
            $mallOrder,
            OpUserEnum::ADMIN,
            $adminUsers->id,
            $rate,
            $content,
            $images
        );
    }

}